<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收款状态">
        <el-select v-model="queryParams.skStatus" placeholder="请选择" clearable>
          <el-option label="未收款" value="1"></el-option>
          <el-option label="已收款" value="2"></el-option>
          <el-option label="已中止" value="3"></el-option>
          <el-option label="已冻结" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账单类型">
        <el-select v-model="queryParams.billType" placeholder="请选择" clearable>
          <el-option label="分期变更" value="1"></el-option>
          <el-option label="租约保理变更" value="2"></el-option>
          <el-option label="退租变更" value="3"></el-option>
          <el-option label="普通账单" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="租房合同编号">
        <el-input v-model="queryParams.contractNo" placeholder="请输入租房合同编号" clearable></el-input>
      </el-form-item>
      <div>
        <el-form-item label="应收日期">
          <div class="block">
            <el-date-picker
              v-model="queryParams.collectionDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              clearable
              placeholder="选择日期时间">
            </el-date-picker>至
            <el-date-picker
              v-model="queryParams.collectionDate1"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="选择日期时间"
              clearable>
            </el-date-picker>
          </div>
        </el-form-item>
      </div>
      <div>
        <el-form-item label="实收日期">
          <div class="block">
            <el-date-picker
              clearable
              v-model="queryParams.realCollectionDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="选择日期时间">
            </el-date-picker>至
            <el-date-picker
              clearable
              v-model="queryParams.realCollectionDate1"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetime"
              placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
      </div>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:plan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['finance:plan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['finance:plan:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs v-model="activeName1" @tab-click="handleClick">
      <el-tab-pane label="房租收款计划" name="first">
        <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="租房合同号" align="center" prop="contractNo" />
          <el-table-column label="收款单号" align="center" prop="receiptOrderNo" />
          <el-table-column label="承租房间(使用房源名称)" align="center" prop="roomName" />
          <el-table-column label="租客姓名" align="center" prop="tenantName" />
          <el-table-column label="小区名称" align="center" prop="premiseName" />
          <el-table-column label="支付金额" align="center" prop="paymentAmount" />
          <el-table-column label="第几期" align="center" prop="receiptNumber" />
          <el-table-column label="账单类型" align="center" prop="billType">
            <template slot-scope="scope">
              <span v-if="scope.row.billType==1">分期变更</span>
              <span v-if="scope.row.billType==2">租约保理变更</span>
              <span v-if="scope.row.billType==3">退租变更</span>
              <span v-if="scope.row.billType==4">普通账单(正常房租租务计划)</span>
            </template>
          </el-table-column>
          <el-table-column label="预定收款日期" align="center" prop="collectionDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.collectionDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="商户ID" align="center" prop="businessId" />
          <el-table-column label="合同ID" align="center" prop="contractId" />
          <el-table-column label="合同类型" align="center" prop="contractType" />
          <el-table-column label="楼盘ID" align="center" prop="premiseId" />
          <el-table-column label="房源ID" align="center" prop="roomId" />
          <el-table-column label="租客ID" align="center" prop="tenantId" />
          <el-table-column label="收款类型" align="center" prop="receiptType" />
          <el-table-column label="款项" align="center" prop="itemType" />
          <el-table-column label="账期开始日" align="center" prop="paymentDaysBegin" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.paymentDaysBegin, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作人" align="center" prop="createUserid" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <span v-if="scope.row.payMethod!=null">
                  --
                </span>
                <el-button
                  size="mini"
                  @click="handlePayment(scope.row)" type="text"
                  v-if="scope.row.payMethod==null">支付</el-button>
              </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="租务收款计划" name="second">租务收款计划</el-tab-pane>
    </el-tabs>

    <!--支付页面-->

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="支付金额">
          <el-input v-model="form.paymentAmount" disabled></el-input>
        </el-form-item>
        <el-form-item label="支付方式">
          <el-select v-model="payMethod" placeholder="请选择支付方式">
            <!--            <el-option :label="item" :value="item" v-for="item in payMethodAll"></el-option>-->
            <el-option value="支付宝" label="支付宝"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="settles">确 定</el-button>
  </span>
    </el-dialog>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listPlan,pay,payMethodList} from "@/api/finance/plan";

export default {
  name: "Plan",
  components: {
  },
  data() {
    return {
      activeName1:"first",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收款计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiptOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        receiptType: null,
        skStatus: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withReceiptPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        collectionDate: null,
        realCollectionDate: null,
        receiptNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      value1:[],
      //付款类型
      payStateAll:[],
      //账单类型
      billTypeAll:[],
      //支付方式
      payMethodAll:[],
      //支付页面 是否显示
      dialogVisible: false,
      //支付方式
      payMethod:null,
      payFormData: {
        wayCode: 'ALI_PC',
        amount: 100,
        currency: 'CNY',
        subject: 'vue测试',
        body: 'vue描述',
        notifyUrl: 'http://43.142.35.71:9218/api/anon/paytestNotify/payOrder',
        payDataType: 'codeImgUrl'
      },
      //支付数据
      form:{
        //支付金额
        paymentAmount:null,
        //商户号
        id:null,
      },
    };
  },
  created() {
    this.getList();
    this.payMethodList();
  },
  methods: {
    /** tabs标签点击事件 */
    handleClick(tab, event) {
      console.log(tab, event);
    },
    /** 查询收款计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取所有支付方式
    payMethodList(){
      payMethodList().then(res=>{
        console.log(res);
        this.payMethodAll = res;
      })
    },
    //支付方法
    settles(row){
      alert(this.payMethod)
      if(this.payMethod=="支付宝" ){
        pay(this.payFormData).then(response=>{
          console.log(response)
          window.open(response.data.data.payData);
          this.dialogVisible = false;
        });
      }
    },
    //支付页面
    handlePayment(row){
      console.log(row.paymentAmount);
      this.form.paymentAmount = row.paymentAmount;
      this.payFormData.amount=row.paymentAmount*100;
      this.form.id = row.id;
      this.dialogVisible = true;
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收款计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收款计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlan(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除收款计划编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPlan(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/plan/export', {
        ...this.queryParams
      }, `finance_plan.xlsx`)
    }
  }
};
</script>
